import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class BSTMap <K extends Comparable<K>,V> implements Map<K,V>{
    @Override
    public void add(K key, V value) {
        this.root = add(root,key,value);
    }

    private Node add(Node node, K key, V value) {
        if (node == null){
            size ++;
            return new Node(key, value);
        }
        if (key.compareTo(node.key) < 0){
            node.left = add(node.left,key,value);
        } else if (key.compareTo(node.key) > 0) {
            node.right = add(node.right,key,value);
        }else {     // key和node.key相等
            node.value = value;
        }
        return node;
    }

    private Node minimum(Node node){
        if (node.left == null){
            return node;
        }
        return minimum(node.left);
    }

    private Node removeMinNode(Node node){
        if (node.left == null){
            Node rightNode = node.right;
            node.right = null;
            size --;
            return rightNode;
        }
        node.left = removeMinNode(node.left);
        return node;
    }
    @Override
    public V remove(K key) {
        Node node = getNode(root, key);
        if (node != null){
            root = remove(root,key);
            return node.value;
        }
        return null;
    }

    private Node remove(Node node,K key){
        if (node == null){
            return null;
        }
        if (key.compareTo(node.key) < 0){
            node.left = remove(node.left,key);
            return node;
        } else if (key.compareTo(node.key) > 0) {
            node.right = remove(node.right,key);
            return node;
        }else {
            // 待删除节点左子树为空的情况
            if (node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size --;
                return rightNode;
            }
            // 待删除节点右子树为空的情况
            if (node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size --;
                return leftNode;
            }
            // 左右子树均不为空的情况
            // 找到比待删除节点大的最小的节点，即待删除节点右子树的最小节点
            // 用这个节点顶替待删除节点的位置
            Node successor = minimum(node.right);
            successor.right = removeMinNode(node.right);
            successor.left = node.left;

            node.right = node.left = null;

            return successor;
        }
    }

    @Override
    public boolean contains(K key) {

        return getNode(root,key) != null;
    }

    private Node getNode(Node node, K key){
        if (node == null){
            return null;
        }
        if (key.compareTo(node.key) == 0){
            return node;
        }else if (key.compareTo(node.key) < 0){
            return getNode(node.left,key);
        }else {
            return getNode(node.right,key);
        }
    }

    @Override
    public V get(K key) {
        Node node = getNode(root, key);
        return node == null? null:node.value;
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(root, key);
        if (node == null){
            throw new IllegalArgumentException(key + "doesn't exist!");
        }
        node.value = newValue;
    }

    @Override
    public int getSize() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }

    /**
     * 二分搜索树的层序遍历
     */
    public void levelOrder(){
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()){
            Node cur = q.remove();
            System.out.println(cur.key+":"+cur.value);

            if (cur.left != null){
                q.add(cur.left);
            }
            if (cur.right != null){
                q.add(cur.right);
            }
        }
    }

    private class Node{
        public K key;

        public V value;

        public Node left,right;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.left = null;
            this.right = null;
        }
    }
    private Node root;
    private int size;

    public BSTMap() {
        root = null;
        size = 0;
    }

    /**
     * 返回以node为根的二分搜索树的最大值所在节点
     * @param node
     * @return
     */
    private Node maximum(Node node) {

        if (node.right == null){
            return node;
        }
        return maximum(node.right);
    }

    /**
     * 二分搜索树的中序遍历
     */
    public void inOrder(){
        inOrder(root);
    }

    /**
     * 中序遍历以node为根的二分搜索树，递归算法
     * @param node
     */
    private void inOrder(Node node) {
        if (node == null){
            return;
        }
        inOrder(node.left);
        System.out.println(node.key+" : "+node.value);
        inOrder(node.right);
    }

    public static void main(String[] args) {
        System.out.println("Pride and Prejudice");

        ArrayList<String> words = new ArrayList<>();

        if (FileOperation.readFile("/Users/liuyifan/IdeaProjects/Data-Structures/07-Map/src/pride-and-prejudice.txt",words)){
            System.out.println("Total words: "+ words.size());

            BSTMap<String, Integer> map = new BSTMap<>();

            for (String word:words){
                if (map.contains(word)){
                    map.set(word, map.get(word)+1);
                }else {
                    map.add(word,1);
                }
            }
            System.out.println("Total different words: " + map.getSize());
            System.out.println("Frequency of PRIDE: " + map.get("pride"));
            System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
        }
    }

//    public static void main(String[] args) {
//        BSTMap<String, Integer> map = new BSTMap<>();
//        map.add("4",33);
//        map.add("99",33);
//        map.add("22",33);
//        map.add("33",33);
//        map.add("44",33);
//
//        map.inOrder();
//        BSTMap<String, Integer>.Node maximum = map.maximum(map.root);
//        System.out.println(maximum.key);
//    }

}
